package com.sinaapp.kltq.dinosaurweather;

import android.app.Fragment;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.KeyEvent;
import android.view.MotionEvent;

import com.sinaapp.kltq.data.CityListItem;
import com.sinaapp.kltq.weathermanager.WeatherManager;

import java.util.List;


public class FlipBlackActivity extends ActionBarActivity
        implements RealtimeFragment.OnFragmentInteractionListener
        , TitleFragment.OnFragmentInteractionListener
        , UpdateTimeFragment.OnFragmentInteractionListener
        , ForecastFragment.OnFragmentInteractionListener
        , ZhishusFragment.OnFragmentInteractionListener {

    public int cityIndex;
    public char pageType;
    public boolean isUpdating;

    private float touchX;
    private float touchY;

    private Fragment contentFragment;

    @Override
    public void onFragmentInteraction(Uri uri) {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_flip_black);
        // get cityIndex and pageType
        Intent intent = getIntent();
        cityIndex = intent.getIntExtra("cityIndex", 0);
        pageType = intent.getCharExtra("pageType", 'r');
        isUpdating = false;
        // put correct fragment to page
        FragmentTransaction transaction;
        transaction = getFragmentManager().beginTransaction();
        Fragment titleFragment = new TitleFragment();
        Fragment updateTimeFragment = new UpdateTimeFragment();
        transaction.replace(R.id.blackHeader, titleFragment);
        transaction.replace(R.id.blackFooter, updateTimeFragment);
        transaction.commit();
        switch (pageType) {
            case 'r':
                transaction = getFragmentManager().beginTransaction();
                contentFragment = new RealtimeFragment();
                transaction.replace(R.id.blackContent, contentFragment);
                transaction.commit();
                break;
            case 'f':
                transaction = getFragmentManager().beginTransaction();
                contentFragment = new ForecastFragment();
                transaction.replace(R.id.blackContent, contentFragment);
                transaction.commit();
                break;
            case 'z':
                transaction = getFragmentManager().beginTransaction();
                contentFragment = new ZhishusFragment();
                transaction.replace(R.id.blackContent, contentFragment);
                transaction.commit();
                break;
        }

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (isUpdating)
            return super.onTouchEvent(event);
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            touchX = event.getX();
            touchY = event.getY();
        } else if (event.getAction() == MotionEvent.ACTION_UP) {
            // 获取滑动参数
            float distanceX = Math.abs(touchX - event.getX());
            float distanceY = Math.abs(touchY - event.getY());
            char direction;
            // 判断滑动距离
            DisplayMetrics dm = new DisplayMetrics();
            getWindowManager().getDefaultDisplay().getMetrics(dm);
            if ((distanceX < dm.widthPixels * 0.216) && (distanceY < dm.heightPixels * 0.173)) {
                return super.onTouchEvent(event);
            }
            // 判断滑动方向
            char newPageType = pageType;
            int newCityIndex = cityIndex;
            // TODO: 制作动画
            int animOut, animIn;
            if (distanceY / distanceX > 1.73) {         // 纵向
                if (touchY - event.getY() > 0) {        // 向上
                    if (pageType == 'r') {
                        newPageType = 'z';
                    } else if (pageType == 'z') {
                        newPageType = 'f';
                    } else {
                        return super.onTouchEvent(event);
                    }
                    animOut = R.anim.abc_slide_out_top;
                    animIn = R.anim.abc_fade_in;
                } else {                                // 向下
                    if (pageType == 'f') {
                        newPageType = 'z';
                    } else if (pageType == 'z') {
                        newPageType = 'r';
                    } else {
                        return super.onTouchEvent(event);
                    }
                    animOut = R.anim.abc_slide_out_bottom;
                    animIn = R.anim.abc_fade_in;
                }
            } else if (distanceY / distanceX < 0.57) {  // 横向
                WeatherManager wm = new WeatherManager(getApplicationContext());
                List<CityListItem> cityList = wm.getCityList();
                if (touchX - event.getX() > 0) {        // 向左
                    newCityIndex = (cityIndex + 1) % cityList.size();
                    animOut = R.anim.abc_fade_out;
                    animIn = R.anim.abc_fade_in;
                } else {                                // 向右
                    newCityIndex = (cityIndex - 1 + cityList.size()) % cityList.size();
                    animOut = R.anim.abc_fade_out;
                    animIn = R.anim.abc_fade_in;
                }
            } else {                                    // 角度过大
                return super.onTouchEvent(event);
            }
            // 换页
            Intent intent = new Intent(FlipBlackActivity.this, FlipWhiteActivity.class);
            intent.putExtra("cityIndex", newCityIndex);
            intent.putExtra("pageType", newPageType);
            startActivity(intent);
            // 设置动画
            overridePendingTransition(animIn, animOut);
            finish();
        }
        return super.onTouchEvent(event);
    }

    @Override
    public void onBackPressed() {
        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_HOME);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
        android.os.Process.killProcess(android.os.Process.myPid());
        super.onBackPressed();
    }

}
